package com.jxs.android.sxyw;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.sxyw.data.OrgInfo;
import com.jxs.android.sxyw.db.OrgHolder;
import com.jxs.android.sxyw.weiget.TopBar;

/**
 * @Des 地区选择
 * @author Jiangxs
 * 
 */
public class SelectOrg extends AbstractActivity {

	private ListView orgListview = null;
	private Context context;
	private List<OrgInfo> cityInfos = new ArrayList<OrgInfo>();
	private List<OrgInfo> coutryInfos = new ArrayList<OrgInfo>();
	private OrgAdpter orgAdpter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.org_with_list);

		context = this;
		cityInfos = OrgHolder.getOrgInfosWithFlag("1");

		orgListview = (ListView) findViewById(R.id.org_listview);
		orgAdpter = new OrgAdpter(context, cityInfos);
		orgListview.setAdapter(orgAdpter);
		orgListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				List<OrgInfo> queryedCoutrys = OrgHolder.getOrgInfosWithParentID(orgAdpter.getOrgInfos().get(position).getId());
				if (null != queryedCoutrys && !queryedCoutrys.isEmpty() && queryedCoutrys.size() > 0) {
					coutryInfos.clear();
					coutryInfos.addAll(queryedCoutrys);
					orgAdpter.setOrgInfos(queryedCoutrys);
					orgAdpter.notifyDataSetChanged();
				} else {
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putString("city_courty_name", OrgHolder.getOrgInfo(coutryInfos.get(position).getPid()).getName() + "  " + coutryInfos.get(position).getName());
					bundle.putString("city_id", coutryInfos.get(position).getPid());
					bundle.putString("coutry_id", coutryInfos.get(position).getId());
					intent.putExtras(bundle);
					setResult(RESULT_OK, intent);
					SelectOrg.this.finish();
				}
			}
		});
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.hindBtn();
		topBar.setTitle("地区选择");
		initTopBarReturn(topBar);
	}

	private class OrgAdpter extends BaseAdapter {

		private LayoutInflater inflater = null;

		private List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();

		public OrgAdpter(Context context, List<OrgInfo> orgInfos) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.orgInfos = orgInfos;
		}

		@Override
		public int getCount() {
			return orgInfos.size();
		}

		@Override
		public OrgInfo getItem(int position) {
			return orgInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView textView = (TextView) inflater.inflate(R.layout.tv_item, null);
			if (position == 0) {
				textView.setBackgroundResource(R.drawable.roundlist_head);
			}
			if (position == orgInfos.size() - 1) {
				textView.setBackgroundResource(R.drawable.roundlist_tail);
			}
			textView.setText("    " + orgInfos.get(position).getName().trim());
			return textView;
		}

		public void setOrgInfos(List<OrgInfo> orgInfos) {
			this.orgInfos = orgInfos;
		}

		public List<OrgInfo> getOrgInfos() {
			return orgInfos;
		}

	}

}